<{extend name="Public:public" /}>

<{block name="title"}>通过 Prop 向子组件传递数据<{/block}>

<{block name="content"}> 
<div class="content guide with-sidebar components-props-guide">

<h1>通过 Prop 向子组件传递数据</h1>

<h2 id="Prop-组件基础"> 
<a href="#Prop-组件基础" class="headerlink" title="Prop-组件基础" data-scroll="">文档基础</a> 
</h2>
<p>父组件中 使用一个<code>props</code> 注册的一些自定义prop特性。当一个值传递给一个 prop 特性的时候，它就变成了那个组件实例的一个属性</p>
<p>一个组件默认可以拥有 <b>任意数量</b>的 prop，<b>任何值都</b>可以传递给任何 prop。在上述模板中，你会发现我们能够在组件实例中访问这个值，<b>就像访问 <code>data</code> 中的值一样</b>。</p>
<p>一个 prop 被注册之后，你就可以像这样把数据作为一个自定义特性传递进来：</p>
<code class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><span class="tag">&lt;template&gt;</span></li><li class="L1"><span class="pln">&nbsp;&nbsp;</span><span class="tag">&lt;blog-post</span><span class="pln">&nbsp;</span><span class="atn">ptitle</span><span class="pun">=</span><span class="atv">"My&nbsp;journey&nbsp;with&nbsp;Vue"</span><span class="tag">&gt;&lt;/blog-post&gt;</span></li><li class="L2"><span class="pln">&nbsp;&nbsp;</span><span class="tag">&lt;blog-post</span><span class="pln">&nbsp;</span><span class="atn">ptitle</span><span class="pun">=</span><span class="atv">"Blogging&nbsp;with&nbsp;Vue"</span><span class="tag">&gt;&lt;/blog-post&gt;</span></li><li class="L3"><span class="pln">&nbsp;&nbsp;</span><span class="tag">&lt;blog-post</span><span class="pln">&nbsp;</span><span class="atn">ptitle</span><span class="pun">=</span><span class="atv">"Why&nbsp;Vue&nbsp;is&nbsp;so&nbsp;fun"</span><span class="tag">&gt;&lt;/blog-post&gt;</span></li><li class="L4"><span class="tag">&lt;/template&gt;</span></li><li class="L5"><span class="pln">&nbsp;</span></li><li class="L6"><span class="tag">&lt;script&gt;</span></li><li class="L7"><span class="pln">&nbsp;&nbsp;</span><span class="typ">Vue</span><span class="pun">.</span><span class="pln">component</span><span class="pun">(</span><span class="str">'blog-post'</span><span class="pun">,</span><span class="pln">&nbsp;</span><span class="pun">{</span></li><li class="L8"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;props</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="pun">[</span><span class="str">'ptitle'</span><span class="pun">],</span></li><li class="L9"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;template</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="str">'&lt;h3&gt;{{&nbsp;title&nbsp;}}&lt;/h3&gt;'</span></li><li class="L0"><span class="pln">&nbsp;&nbsp;</span><span class="pun">})</span></li><li class="L1"><span class="tag">&lt;/script&gt;</span></li></ol></code>



<p>然而在一个典型的应用中，你可能在 <code>data</code> 里有一个博文的 <code>数组</code>, 并想要为每篇博文渲染一个组件：</p>
<code class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><span class="tag">&lt;template&gt;</span></li><li class="L1"><span class="pln">&nbsp;&nbsp;</span><span class="tag">&lt;blog-post</span></li><li class="L2"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="atn">v-for</span><span class="pun">=</span><span class="atv">"post&nbsp;in&nbsp;posts"</span><span class="pln">&nbsp;:</span><span class="atn">key</span><span class="pun">=</span><span class="atv">"post.id"</span></li><li class="L3"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;:</span><span class="atn">ptitle</span><span class="pun">=</span><span class="atv">"post.title"</span></li><li class="L4"><span class="pln">&nbsp;&nbsp;</span><span class="tag">&gt;&lt;/blog-post&gt;</span></li><li class="L5"><span class="tag">&lt;/template&gt;</span></li><li class="L6"><span class="pln">&nbsp;</span></li><li class="L7"><span class="tag">&lt;script&gt;</span></li><li class="L8"><span class="pln">&nbsp;&nbsp;</span><span class="typ">Vue</span><span class="pun">.</span><span class="pln">component</span><span class="pun">(</span><span class="str">'blog-post'</span><span class="pun">,</span><span class="pln">&nbsp;</span><span class="pun">{</span></li><li class="L9"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;props</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="pun">[</span><span class="str">'ptitle'</span><span class="pun">],</span></li><li class="L0"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;template</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="str">'&lt;h3&gt;{{&nbsp;title&nbsp;}}&lt;/h3&gt;'</span></li><li class="L1"><span class="pln">&nbsp;&nbsp;</span><span class="pun">})</span></li><li class="L2"><span class="pln">&nbsp;&nbsp;</span><span class="kwd">new</span><span class="pln">&nbsp;</span><span class="typ">Vue</span><span class="pun">({</span></li><li class="L3"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;el</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="str">'#blog-post-demo'</span><span class="pun">,</span></li><li class="L4"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;data</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="pun">{</span></li><li class="L5"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;posts</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="pun">[</span></li><li class="L6"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun">{</span><span class="pln">&nbsp;id</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="lit">1</span><span class="pun">,</span><span class="pln">&nbsp;title</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="str">'My&nbsp;journey&nbsp;with&nbsp;Vue'</span><span class="pln">&nbsp;</span><span class="pun">},</span></li><li class="L7"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun">{</span><span class="pln">&nbsp;id</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="lit">2</span><span class="pun">,</span><span class="pln">&nbsp;title</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="str">'Blogging&nbsp;with&nbsp;Vue'</span><span class="pln">&nbsp;</span><span class="pun">},</span></li><li class="L8"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun">{</span><span class="pln">&nbsp;id</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="lit">3</span><span class="pun">,</span><span class="pln">&nbsp;title</span><span class="pun">:</span><span class="pln">&nbsp;</span><span class="str">'Why&nbsp;Vue&nbsp;is&nbsp;so&nbsp;fun'</span><span class="pln">&nbsp;</span><span class="pun">}</span></li><li class="L9"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun">]</span></li><li class="L0"><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun">}</span></li><li class="L1"><span class="pln">&nbsp;&nbsp;</span><span class="pun">})</span></li><li class="L2"><span class="tag">&lt;/script&gt;</span></li></ol></code>

<p>如上所示，你会发现我们可以使用 <code>v-bind</code> 来动态传递 prop。这在你一开始不清楚要渲染的具体内容，比如<a href="https://jsfiddle.net/chrisvfritz/sbLgr0ad" target="_blank" rel="noopener">从一个 API 获取博文列表</a>的时候，是非常有用的。</p>
<p>到目前为止，关于 prop 你需要了解的大概就这些了，如果你阅读完本页内容并掌握了它的内容，我们会推荐你再回来把 <a href="components-props.html">prop</a> 读完。</p>




<h2 id="Prop-的大小写-camelCase-vs-kebab-case"> 
	<a href="#Prop-的大小写-camelCase-vs-kebab-case" class="headerlink" title="Prop 的大小写 (camelCase vs kebab-case)" data-scroll="">Prop 的大小写 (camelCase vs kebab-case)</a></h2><p>HTML 中的特性名是大小写不敏感的，所以浏览器会把所有大写字符解释为小写字符。这意味着当你使用 DOM 中的模板时，camelCase (驼峰命名法) 的 prop 名需要使用其等价的 kebab-case (短横线分隔命名) 命名：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'blog-post'</span>, {</span><br><span class="line">  <span class="comment">// 在 JavaScript 中是 camelCase 的</span></span><br><span class="line">  props: [<span class="string">'postTitle'</span>],</span><br><span class="line">  template: <span class="string">'&lt;h3&gt;{{ postTitle }}&lt;/h3&gt;'</span></span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 在 HTML 中是 kebab-case 的 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">post-title</span>=<span class="string">"hello!"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>重申一次，如果你使用字符串模板，那么这个限制就不存在了。</p>
<h2 id="Prop-类型"><a href="#Prop-类型" class="headerlink" title="Prop 类型" data-scroll="">Prop 类型</a></h2><p>到这里，我们只看到了以字符串数组形式列出的 prop：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">props: [<span class="string">'title'</span>, <span class="string">'likes'</span>, <span class="string">'isPublished'</span>, <span class="string">'commentIds'</span>, <span class="string">'author'</span>]</span><br></pre></td></tr></tbody></table></figure>
<p>但是，通常你希望每个 prop 都有指定的值类型。这时，你可以以对象形式列出 prop，这些属性的名称和值分别是 prop 各自的名称和类型：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">props: {</span><br><span class="line">  title: <span class="built_in">String</span>,</span><br><span class="line">  likes: <span class="built_in">Number</span>,</span><br><span class="line">  isPublished: <span class="built_in">Boolean</span>,</span><br><span class="line">  commentIds: <span class="built_in">Array</span>,</span><br><span class="line">  author: <span class="built_in">Object</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>这不仅为你的组件提供了文档，还会在它们遇到错误的类型时从浏览器的 JavaScript 控制台提示用户。你会在这个页面接下来的部分看到<a href="#Prop-验证">类型检查和其它 prop 验证</a>。</p>
<h2 id="传递静态或动态-Prop"><a href="#传递静态或动态-Prop" class="headerlink" title="传递静态或动态 Prop" data-scroll="">传递静态或动态 Prop</a></h2><p>像这样，你已经知道了可以像这样给 prop 传入一个静态的值：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">title</span>=<span class="string">"My journey with Vue"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>你也知道 prop 可以通过 <code>v-bind</code> 动态赋值，例如：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 动态赋予一个变量的值 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:title</span>=<span class="string">"post.title"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 动态赋予一个复杂表达式的值 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:title</span>=<span class="string">"post.title + ' by ' + post.author.name"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>在上述两个示例中，我们传入的值都是字符串类型的，但实际上<em>任何</em>类型的值都可以传给一个 prop。</p>
<h3 id="传入一个数字"><a href="#传入一个数字" class="headerlink" title="传入一个数字" data-scroll="">传入一个数字</a></h3><figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 即便 `42` 是静态的，我们仍然需要 `v-bind` 来告诉 Vue --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 这是一个 JavaScript 表达式而不是一个字符串。--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:likes</span>=<span class="string">"42"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 用一个变量进行动态赋值。--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:likes</span>=<span class="string">"post.likes"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="传入一个布尔值"><a href="#传入一个布尔值" class="headerlink" title="传入一个布尔值" data-scroll="">传入一个布尔值</a></h3><figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 包含该 prop 没有值的情况在内，都意味着 `true`。--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">is-published</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 即便 `false` 是静态的，我们仍然需要 `v-bind` 来告诉 Vue --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 这是一个 JavaScript 表达式而不是一个字符串。--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:is-published</span>=<span class="string">"false"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 用一个变量进行动态赋值。--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:is-published</span>=<span class="string">"post.isPublished"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="传入一个数组"><a href="#传入一个数组" class="headerlink" title="传入一个数组" data-scroll="">传入一个数组</a></h3><figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 即便数组是静态的，我们仍然需要 `v-bind` 来告诉 Vue --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 这是一个 JavaScript 表达式而不是一个字符串。--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:comment-ids</span>=<span class="string">"[234, 266, 273]"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 用一个变量进行动态赋值。--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:comment-ids</span>=<span class="string">"post.commentIds"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="传入一个对象"><a href="#传入一个对象" class="headerlink" title="传入一个对象" data-scroll="">传入一个对象</a></h3><figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 即便对象是静态的，我们仍然需要 `v-bind` 来告诉 Vue --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 这是一个 JavaScript 表达式而不是一个字符串。--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:author</span>=<span class="string">"{ name: 'Veronica', company: 'Veridian Dynamics' }"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 用一个变量进行动态赋值。--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind:author</span>=<span class="string">"post.author"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="传入一个对象的所有属性"><a href="#传入一个对象的所有属性" class="headerlink" title="传入一个对象的所有属性" data-scroll="">传入一个对象的所有属性</a></h3><p>如果你想要将一个对象的所有属性都作为 prop 传入，你可以使用不带参数的 <code>v-bind</code> (取代 <code>v-bind:prop-name</code>)。例如，对于一个给定的对象 <code>post</code>：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">post: {</span><br><span class="line">  id: <span class="number">1</span>,</span><br><span class="line">  title: <span class="string">'My Journey with Vue'</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>下面的模板：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">blog-post</span> <span class="attr">v-bind</span>=<span class="string">"post"</span>&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>等价于：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">blog-post</span></span></span><br><span class="line"><span class="tag">  <span class="attr">v-bind:id</span>=<span class="string">"post.id"</span></span></span><br><span class="line"><span class="tag">  <span class="attr">v-bind:title</span>=<span class="string">"post.title"</span></span></span><br><span class="line"><span class="tag">&gt;</span><span class="tag">&lt;/<span class="name">blog-post</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<h2 id="单向数据流"><a href="#单向数据流" class="headerlink" title="单向数据流" data-scroll="">单向数据流</a></h2><p>所有的 prop 都使得其父子 prop 之间形成了一个<strong>单向下行绑定</strong>：父级 prop 的更新会向下流动到子组件中，但是反过来则不行。这样会防止从子组件意外改变父级组件的状态，从而导致你的应用的数据流向难以理解。</p>
<p>额外的，每次父级组件发生更新时，子组件中所有的 prop 都将会刷新为最新的值。这意味着你<strong>不</strong>应该在一个子组件内部改变 prop。如果你这样做了，Vue 会在浏览器的控制台中发出警告。</p>
<p>这里有两种常见的试图改变一个 prop 的情形：</p>
<ol>
<li><p><strong>这个 prop 用来传递一个初始值；这个子组件接下来希望将其作为一个本地的 prop 数据来使用。</strong>在这种情况下，最好定义一个本地的 data 属性并将这个 prop 用作其初始值：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">props: [<span class="string">'initialCounter'</span>],</span><br><span class="line">data: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">  <span class="keyword">return</span> {</span><br><span class="line">    counter: <span class="keyword">this</span>.initialCounter</span><br><span class="line">  }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
</li>
<li><p><strong>这个 prop 以一种原始的值传入且需要进行转换。</strong>在这种情况下，最好使用这个 prop 的值来定义一个计算属性：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">props: [<span class="string">'size'</span>],</span><br><span class="line">computed: {</span><br><span class="line">  normalizedSize: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.size.trim().toLowerCase()</span><br><span class="line">  }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
</li>
</ol>
<p class="tip">注意在 JavaScript 中对象和数组是通过引用传入的，所以对于一个数组或对象类型的 prop 来说，在子组件中改变这个对象或数组本身<strong>将会</strong>影响到父组件的状态。</p>

<h2 id="Prop-验证"><a href="#Prop-验证" class="headerlink" title="Prop 验证" data-scroll="">Prop 验证</a></h2><p>我们可以为组件的 prop 指定验证要求，例如你知道的这些类型。如果有一个需求没有被满足，则 Vue 会在浏览器控制台中警告你。这在开发一个会被别人用到的组件时尤其有帮助。</p>
<p>为了定制 prop 的验证方式，你可以为 <code>props</code> 中的值提供一个带有验证需求的对象，而不是一个字符串数组。例如：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'my-component'</span>, {</span><br><span class="line">  props: {</span><br><span class="line">    <span class="comment">// 基础的类型检查 (`null` 匹配任何类型)</span></span><br><span class="line">    propA: <span class="built_in">Number</span>,</span><br><span class="line">    <span class="comment">// 多个可能的类型</span></span><br><span class="line">    propB: [<span class="built_in">String</span>, <span class="built_in">Number</span>],</span><br><span class="line">    <span class="comment">// 必填的字符串</span></span><br><span class="line">    propC: {</span><br><span class="line">      type: <span class="built_in">String</span>,</span><br><span class="line">      required: <span class="literal">true</span></span><br><span class="line">    },</span><br><span class="line">    <span class="comment">// 带有默认值的数字</span></span><br><span class="line">    propD: {</span><br><span class="line">      type: <span class="built_in">Number</span>,</span><br><span class="line">      <span class="keyword">default</span>: <span class="number">100</span></span><br><span class="line">    },</span><br><span class="line">    <span class="comment">// 带有默认值的对象</span></span><br><span class="line">    propE: {</span><br><span class="line">      type: <span class="built_in">Object</span>,</span><br><span class="line">      <span class="comment">// 对象或数组默认值必须从一个工厂函数获取</span></span><br><span class="line">      <span class="keyword">default</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">        <span class="keyword">return</span> { <span class="attr">message</span>: <span class="string">'hello'</span> }</span><br><span class="line">      }</span><br><span class="line">    },</span><br><span class="line">    <span class="comment">// 自定义验证函数</span></span><br><span class="line">    propF: {</span><br><span class="line">      validator: <span class="function"><span class="keyword">function</span> (<span class="params">value</span>) </span>{</span><br><span class="line">        <span class="comment">// 这个值必须匹配下列字符串中的一个</span></span><br><span class="line">        <span class="keyword">return</span> [<span class="string">'success'</span>, <span class="string">'warning'</span>, <span class="string">'danger'</span>].indexOf(value) !== <span class="number">-1</span></span><br><span class="line">      }</span><br><span class="line">    }</span><br><span class="line">  }</span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<p>当 prop 验证失败的时候，(开发环境构建版本的) Vue 将会产生一个控制台的警告。</p>
<p class="tip">注意那些 prop 会在一个组件实例创建<strong>之前</strong>进行验证，所以实例的属性 (如 <code>data</code>、<code>computed</code> 等) 在 <code>default</code> 或 <code>validator</code> 函数中是不可用的。</p>

<h3 id="类型检查"><a href="#类型检查" class="headerlink" title="类型检查" data-scroll="">类型检查</a></h3><p><code>type</code> 可以是下列原生构造函数中的一个：</p>
<ul>
<li><code>String</code></li>
<li><code>Number</code></li>
<li><code>Boolean</code></li>
<li><code>Array</code></li>
<li><code>Object</code></li>
<li><code>Date</code></li>
<li><code>Function</code></li>
<li><code>Symbol</code></li>
</ul>
<p>额外的，<code>type</code> 还可以是一个自定义的构造函数，并且通过 <code>instanceof</code> 来进行检查确认。例如，给定下列现成的构造函数：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span> (<span class="params">firstName, lastName</span>) </span>{</span><br><span class="line">  <span class="keyword">this</span>.firstName = firstName</span><br><span class="line">  <span class="keyword">this</span>.lastName = lastName</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>你可以使用：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'blog-post'</span>, {</span><br><span class="line">  props: {</span><br><span class="line">    author: Person</span><br><span class="line">  }</span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<p>来验证 <code>author</code> prop 的值是否是通过 <code>new Person</code> 创建的。</p>


<h2 id="非-Prop-的特性"> 
	<a href="#非-Prop-的特性" class="headerlink" title="非 Prop 的特性" data-scroll="">非 Prop 的特性</a> 
</h2> 
<p>一个非 prop 特性是指传向一个组件，但是该组件并没有相应 prop 定义的特性。</p>
<p>因为显式定义的 prop 适用于向一个子组件传入信息，然而组件库的作者并不总能预见组件会被用于怎样的场景。这也是为什么组件可以接受任意的特性，而这些特性会被添加到这个组件的根元素上。</p>
<p>例如，想象一下你通过一个 Bootstrap 插件使用了一个第三方的 <code>&lt;bootstrap-date-input&gt;</code> 组件，这个插件需要在其 <code>&lt;input&gt;</code> 上用到一个 <code>data-date-picker</code> 特性。我们可以将这个特性添加到你的组件实例上：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bootstrap-date-input</span> <span class="attr">data-date-picker</span>=<span class="string">"activated"</span>&gt;</span><span class="tag">&lt;/<span class="name">bootstrap-date-input</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>然后这个 <code>data-date-picker="activated"</code> 特性就会自动添加到 <code>&lt;bootstrap-date-input&gt;</code> 的根元素上。</p>
<blockquote>
	<div>van: 预先设计好的子组件如果没有 对应的 pros:[{}]处理, 则 父组件 传递的特性 就以 <b>属性</b> 的形式 解析在 子组件代码上</div>
</blockquote>



<h3 id="替换-合并已有的特性"> 
	<a href="#替换-合并已有的特性" class="headerlink" title="替换/合并已有的特性" data-scroll="">替换/合并已有的特性</a> 
</h3> 
<p>想象一下 <code>&lt;bootstrap-date-input&gt;</code> 的模板是这样的：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"date"</span> <span class="attr">class</span>=<span class="string">"form-control"</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>为了给我们的日期选择器插件定制一个主题，我们可能需要像这样添加一个特别的类名：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bootstrap-date-input</span></span></span><br><span class="line"><span class="tag">  <span class="attr">data-date-picker</span>=<span class="string">"activated"</span></span></span><br><span class="line"><span class="tag">  <span class="attr">class</span>=<span class="string">"date-picker-theme-dark"</span></span></span><br><span class="line"><span class="tag">&gt;</span><span class="tag">&lt;/<span class="name">bootstrap-date-input</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>在这种情况下，我们定义了两个不同的 <code>class</code> 的值：</p>
<ul>
<li><code>form-control</code>，这是在组件的模板内设置好的</li>
<li><code>date-picker-theme-dark</code>，这是从组件的父级传入的</li>
</ul>
<p>对于绝大多数特性来说，从外部提供给组件的值会替换掉组件内部设置好的值。所以如果传入 <code>type="text"</code> 就会替换掉 <code>type="date"</code> 并把它破坏！庆幸的是，<code>class</code> 和 <code>style</code> 特性会稍微智能一些，即两边的值会被合并起来，从而得到最终的值：<code>form-control date-picker-theme-dark</code>。</p>
<h3 id="禁用特性继承"><a href="#禁用特性继承" class="headerlink" title="禁用特性继承" data-scroll="">禁用特性继承</a></h3><p>如果你<strong>不</strong>希望组件的根元素继承特性，你可以在组件的选项中设置 <code>inheritAttrs: false</code>。例如：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'my-component'</span>, {</span><br><span class="line">  inheritAttrs: <span class="literal">false</span>,</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<p>这尤其适合配合实例的 <code>$attrs</code> 属性使用，该属性包含了传递给一个组件的特性名和特性值，例如：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">{</span><br><span class="line">  class: 'username-input',</span><br><span class="line">  placeholder: <span class="string">'Enter your username'</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>有了 <code>inheritAttrs: false</code> 和 <code>$attrs</code>，你就可以手动决定这些特性会被赋予哪个元素。在撰写<a href="../style-guide/#基础组件名-强烈推荐">基础组件</a>的时候是常会用到的：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'base-input'</span>, {</span><br><span class="line">  inheritAttrs: <span class="literal">false</span>,</span><br><span class="line">  props: [<span class="string">'label'</span>, <span class="string">'value'</span>],</span><br><span class="line">  template: <span class="string">`</span></span><br><span class="line"><span class="string">    &lt;label&gt;</span></span><br><span class="line"><span class="string">      {{ label }}</span></span><br><span class="line"><span class="string">      &lt;input</span></span><br><span class="line"><span class="string">        v-bind="$attrs"</span></span><br><span class="line"><span class="string">        v-bind:value="value"</span></span><br><span class="line"><span class="string">        v-on:input="$emit('input', $event.target.value)"</span></span><br><span class="line"><span class="string">      &gt;</span></span><br><span class="line"><span class="string">    &lt;/label&gt;</span></span><br><span class="line"><span class="string">  `</span></span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<p>这个模式允许你在使用基础组件的时候更像是使用原始的 HTML 元素，而不会担心哪个元素是真正的根元素：</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">base-input</span></span></span><br><span class="line"><span class="tag">  <span class="attr">v-model</span>=<span class="string">"username"</span></span></span><br><span class="line"><span class="tag">  <span class="attr">class</span>=<span class="string">"username-input"</span></span></span><br><span class="line"><span class="tag">  <span class="attr">placeholder</span>=<span class="string">"Enter your username"</span></span></span><br><span class="line"><span class="tag">&gt;</span><span class="tag">&lt;/<span class="name">base-input</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>

  
  
    <div class="guide-links">
      
      
        <span>← <a href="/v2/guide/components-registration.html">组件注册</a></span>
      
      
      
        <span style="float: right;"><a href="/v2/guide/components-custom-events.html">自定义事件</a> →</span>
      
    </div>
  
  <div class="footer">
      <script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
<div class="bsa-cpc"></div>
<script>
  (function(){
    if(typeof _bsa !== 'undefined' && _bsa) {
    _bsa.init('default', 'CKYD62QM', 'placement:vuejsorg', {
      target: '.bsa-cpc',
      align: 'horizontal',
      disable_css: 'true'
    });
      }
  })();
</script>

    发现错误？想参与编辑？
    <a href="https://github.com/vuejs/cn.vuejs.org/blob/master/src/v2/guide/components-props.md" target="_blank">
      在 GitHub 上编辑此页！
    </a>
  </div>
</div>
<{/block}>
